package com.ruoyi.web.controller.upload.service.Impl;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.constant.UploadConstants;
import com.ruoyi.common.entity.DO.FileDO;
import com.ruoyi.common.entity.VO.FileSearchVO;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.web.controller.system.cloud.CloudStorageService;
import com.ruoyi.web.controller.system.cloud.OSSFactory;
import com.ruoyi.web.controller.upload.mapper.IUploadMapper;
import com.ruoyi.web.controller.upload.service.IUploadService;
import org.apache.commons.fileupload.FileUploadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@Service
public class UploadServiceImpl implements IUploadService {

    @Autowired
    private IUploadMapper uploadMapper;

    @Override
    public List<FileDO> searchList(FileSearchVO fileSearchVO) {
        return uploadMapper.searchList(fileSearchVO);
    }

    @Override
    public FileDO addImageFile(MultipartFile file, Integer modelType, String createBy, Long shopId) {
        String uploadPath;
        switch (UploadConstants.ModelTypeEnum.match(modelType)) {
            case NULL:
            default:
                uploadPath = UploadConstants.ModelTypeEnum.match(modelType).getUploadPath();
        }
        try {
            FileDO fileDO = new FileDO();
            CloudStorageService cloudStorageService = OSSFactory.build();
            String filePath = FileUploadUtils.getFilePath(uploadPath + "/" + shopId, file, MimeTypeUtils.IMAGE_EXTENSION);
            String url = cloudStorageService.upload(file.getInputStream(), "glht" + filePath);
//            String filePath = FileUploadUtils.upload(uploadPath, file, MimeTypeUtils.IMAGE_EXTENSION);
//            fileDO.setModelType(modelType);
//            fileDO.setFileName(file.getOriginalFilename());
//            fileDO.setExt(FileUploadUtils.getExtension(file));
//            fileDO.setSize(file.getSize());
//            fileDO.setUrl(filePath);
//            fileDO.setCreateBy(createBy);
//            fileDO.setCreateTime(new Date());
            fileDO.setUrl(url);
//            uploadMapper.insertSelective(fileDO);
            return fileDO;
        } catch (FileUploadException e) {
            throw new BusinessException("超出大小限制");
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(e.getMessage());
        }
    }

    @Override
    public FileDO addCertFile(MultipartFile file, Long shopId) {
        try {
            FileDO fileDO = new FileDO();
            int fileNameLength = file.getOriginalFilename().length();
            if (fileNameLength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) {
                throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
            }
            FileUploadUtils.assertAllowed(file, new String[]{"p12"}, FileUploadUtils.DEFAULT_MAX_SIZE);
            String url = Global.getProfile() + "/cert/" + shopId + "/apiclient_cert.p12";
            File desc = FileUploadUtils.getAbsoluteFile(Global.getProfile() + "/cert/" + shopId, "apiclient_cert.p12");
            file.transferTo(desc);
            fileDO.setUrl(url.substring(10));
            return fileDO;
        } catch (FileUploadException e) {
            throw new BusinessException("超出大小限制");
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(e.getMessage());
        }
    }
}
